home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Online / SpeakFreely / src / adpcm / adpcm-u.c next >
C/C++ Source or Header  |  2000-05-18  |  8KB  |  261 lines

  1. /***********************************************************
  2. Copyright 1992 by Stichting Mathematisch Centrum, Amsterdam, The
  3. Netherlands.
  4.  
  5.             All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI not be used in advertising or publicity pertaining to
  13. distribution of the software without specific, written prior permission.
  14.  
  15. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  18. FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25. /*
  26. ** Intel/DVI ADPCM coder/decoder.
  27. **
  28. ** The algorithm for this coder was taken from the IMA Compatibility Project
  29. ** proceedings, Vol 2, Number 2; May 1992.
  30. **
  31. ** Version 1.2, 18-Dec-92.
  32. **
  33. ** Change log:
  34.  
  35.     Modified by John Walker in September 1995 for use in
  36.     Speak Freely.  The only change was to make the encoder
  37.     and decoder work directly on u-law encoded samples.
  38.     This permits splicing it into the compression chain
  39.     at the proper point, after possible 2X compression has
  40.     been performed.
  41.  
  42. ** - Fixed a stupid bug, where the delta was computed as
  43. **   stepsize*code/4 in stead of stepsize*(code+0.5)/4.
  44. ** - There was an off-by-one error causing it to pick
  45. **   an incorrect delta once in a blue moon.
  46. ** - The NODIVMUL define has been removed. Computations are now always done
  47. **   using shifts, adds and subtracts. It turned out that, because the standard
  48. **   is defined using shift/add/subtract, you needed bits of fixup code
  49. **   (because the div/mul simulation using shift/add/sub made some rounding
  50. **   errors that real div/mul don't make) and all together the resultant code
  51. **   ran slower than just using the shifts all the time.
  52. ** - Changed some of the variable names to be more meaningful.
  53. */
  54.  
  55. #include "adpcm-u.h"
  56. #include "ulaw2linear.h"
  57.  
  58. #ifndef __STDC__
  59. #define signed
  60. #endif
  61.  
  62. /* Intel ADPCM step variation table */
  63. static int indexTable[16] = {
  64.     -1, -1, -1, -1, 2, 4, 6, 8,
  65.     -1, -1, -1, -1, 2, 4, 6, 8,
  66. };
  67.  
  68. static int stepsizeTable[89] = {
  69.     7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
  70.     19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
  71.     50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
  72.     130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
  73.     337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
  74.     876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
  75.     2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
  76.     5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
  77.     15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
  78. };
  79.     
  80. void
  81. adpcm_coder_u(indata, outdata, len, state)
  82.     unsigned char indata[];
  83.     char outdata[];
  84.     int len;
  85.     struct adpcm_state *state;
  86. {
  87.     unsigned char *inp;     /* Input buffer pointer */
  88.     signed char *outp;        /* output buffer pointer */
  89.     int val;            /* Current input sample value */
  90.     int sign;            /* Current adpcm sign bit */
  91.     int delta;            /* Current adpcm output value */
  92.     long diff;            /* Difference between val and valprev */
  93.     int step;            /* Stepsize */
  94.     long valpred;        /* Predicted output value */
  95.     int vpdiff;         /* Current change to valpred */
  96.     int index;            /* Current step change index */
  97.     int outputbuffer;        /* place to keep previous 4-bit value */
  98.     int bufferstep;        /* toggle between outputbuffer/output */
  99.  
  100.     outp = (signed char *)outdata;
  101.     inp = indata;
  102.  
  103.     valpred = state->valprev;
  104.     index = state->index;
  105.     step = stepsizeTable[index];
  106.     
  107.     bufferstep = 1;
  108.  
  109.     for ( ; len > 0 ; len-- ) {
  110.     val = audio_u2s(*inp++);
  111.  
  112.     /* Step 1 - compute difference with previous value */
  113.     diff = val - valpred;
  114.     sign = (diff < 0) ? 8 : 0;
  115.     if ( sign ) diff = (-diff);
  116.  
  117.     /* Step 2 - Divide and clamp */
  118.     /* Note:
  119.     ** This code *approximately* computes:
  120.     **    delta = diff*4/step;
  121.     **    vpdiff = (delta+0.5)*step/4;
  122.     ** but in shift step bits are dropped. The net result of this is
  123.     ** that even if you have fast mul/div hardware you cannot put it to
  124.     ** good use since the fixup would be too expensive.
  125.     */
  126.     delta = 0;
  127.     vpdiff = (step >> 3);
  128.     
  129.     if ( diff >= step ) {
  130.         delta = 4;
  131.         diff -= step;
  132.         vpdiff += step;
  133.     }
  134.     step >>= 1;
  135.     if ( diff >= step  ) {
  136.         delta |= 2;
  137.         diff -= step;
  138.         vpdiff += step;
  139.     }
  140.     step >>= 1;
  141.     if ( diff >= step ) {
  142.         delta |= 1;
  143.         vpdiff += step;
  144.     }
  145.  
  146.     /* Step 3 - Update previous value */
  147.     if ( sign )
  148.       valpred -= vpdiff;
  149.     else
  150.       valpred += vpdiff;
  151.  
  152.     /* Step 4 - Clamp previous value to 16 bits */
  153.     if ( valpred > 32767L )
  154.       valpred = 32767L;
  155.     else if ( valpred < -32768L )
  156.       valpred = -32768L;
  157.  
  158.     /* Step 5 - Assemble value, update index and step values */
  159.     delta |= sign;
  160.     
  161.     index += indexTable[delta];
  162.     if ( index < 0 ) index = 0;
  163.     if ( index > 88 ) index = 88;
  164.     step = stepsizeTable[index];
  165.  
  166.     /* Step 6 - Output value */
  167.     if ( bufferstep ) {
  168.         outputbuffer = (delta << 4) & 0xf0;
  169.     } else {
  170.         *outp++ = (delta & 0x0f) | outputbuffer;
  171.     }
  172.     bufferstep = !bufferstep;
  173.     }
  174.  
  175.     /* Output last step, if needed */
  176.     if ( !bufferstep )
  177.       *outp++ = outputbuffer;
  178.     
  179.     state->valprev = (short) valpred;
  180.     state->index = index;
  181. }
  182.  
  183. void
  184. adpcm_decoder_u(indata, outdata, len, state)
  185.     char indata[];
  186.     unsigned char outdata[];
  187.     int len;
  188.     struct adpcm_state *state;
  189. {
  190.     signed char *inp;        /* Input buffer pointer */
  191.     unsigned char *outp;    /* Output buffer pointer */
  192.     int sign;            /* Current adpcm sign bit */
  193.     int delta;            /* Current adpcm output value */
  194.     int step;            /* Stepsize */
  195.     long valpred;        /* Predicted value */
  196.     int vpdiff;         /* Current change to valpred */
  197.     int index;            /* Current step change index */
  198.     int inputbuffer;        /* place to keep next 4-bit value */
  199.     int bufferstep;        /* toggle between inputbuffer/input */
  200.  
  201.     outp = outdata;
  202.     inp = (signed char *)indata;
  203.  
  204.     valpred = state->valprev;
  205.     index = state->index;
  206.     step = stepsizeTable[index];
  207.  
  208.     bufferstep = 0;
  209.     
  210.     for ( ; len > 0 ; len-- ) {
  211.     
  212.     /* Step 1 - get the delta value */
  213.     if ( bufferstep ) {
  214.         delta = inputbuffer & 0xf;
  215.     } else {
  216.         inputbuffer = *inp++;
  217.         delta = (inputbuffer >> 4) & 0xf;
  218.     }
  219.     bufferstep = !bufferstep;
  220.  
  221.     /* Step 2 - Find new index value (for later) */
  222.     index += indexTable[delta];
  223.     if ( index < 0 ) index = 0;
  224.     if ( index > 88 ) index = 88;
  225.  
  226.     /* Step 3 - Separate sign and magnitude */
  227.     sign = delta & 8;
  228.     delta = delta & 7;
  229.  
  230.     /* Step 4 - Compute difference and new predicted value */
  231.     /*
  232.         ** Computes 'vpdiff = (delta+0.5)*step/4', but see comment
  233.     ** in adpcm_coder.
  234.     */
  235.     vpdiff = step >> 3;
  236.     if ( delta & 4 ) vpdiff += step;
  237.     if ( delta & 2 ) vpdiff += step>>1;
  238.     if ( delta & 1 ) vpdiff += step>>2;
  239.  
  240.     if ( sign )
  241.       valpred -= vpdiff;
  242.     else
  243.       valpred += vpdiff;
  244.  
  245.     /* Step 5 - clamp output value */
  246.     if ( valpred > 32767L )
  247.       valpred = 32767L;
  248.     else if ( valpred < -32768L )
  249.       valpred = -32768L;
  250.  
  251.     /* Step 6 - Update step value */
  252.     step = stepsizeTable[index];
  253.  
  254.     /* Step 7 - Output value */
  255.     *outp++ = audio_s2u(valpred);
  256.     }
  257.  
  258.     state->valprev = (short) valpred;
  259.     state->index = index;
  260. }
  261.